package com.bingxue.edu.parent.controller;

import com.bingxue.edu.common.enums.OperateSourceEnum;
import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.management.shop.model.query.ParentOrderQuery;
import com.bingxue.edu.management.shop.model.req.OrderCreateReq;
import com.bingxue.edu.management.shop.model.req.OrderOnlinePayReq;
import com.bingxue.edu.management.shop.model.req.OrderPriceCalculateReq;
import com.bingxue.edu.management.shop.model.resp.OrderPriceDetail;
import com.bingxue.edu.management.shop.service.OrderService;
import com.bingxue.edu.parent.model.query.StudentRefundQuery;
import com.bingxue.edu.parent.model.resp.ParentOrderResp;
import com.bingxue.edu.parent.model.resp.StudentRefundResp;
import com.bingxue.edu.parent.service.ParentShopService;
import com.bingxue.edu.parent.service.StudentOrderService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author panbo
 * @since 2024/10/28
 */
@Tag(name = "家长端小程序-订单接口")
@RestController
@RequestMapping("/p/order")
@RequiredArgsConstructor
public class ParentOrderController extends ParentBaseController {

    private final ParentShopService parentShopService;
    private final OrderService orderService;
    private final StudentOrderService studentOrderService;

    @Operation(summary = "计算订单价格")
    @PostMapping("/price")
    public Response<OrderPriceDetail> calculatePrice(@RequestBody @Validated OrderPriceCalculateReq req) {
        this.checkStudentRelation(req.getStudentId());
        req.setOperateSource(OperateSourceEnum.PARENT);
        return Response.success(orderService.calculatePrice(req));
    }


    @Operation(summary = "创建订单")
    @PostMapping("/create")
    public Response<String> createOrder(@RequestBody @Validated OrderCreateReq req) {
        this.checkStudentRelation(req.getStudentId());
        req.setOperateSource(OperateSourceEnum.PARENT);
        return Response.success(parentShopService.createOrder(req));
    }

    @Operation(summary = "查询订单")
    @GetMapping("/page")
    public Response<Pagination<ParentOrderResp>> getOrderPage(@Validated ParentOrderQuery query, @Validated PageQuery pageQuery) {
        this.checkStudentRelation(query.getStudentId());
        Page<ParentOrderResp> page = studentOrderService.getOrderPage(query, pageQuery);
        return Response.success(Pagination.of(page));
    }

    @Operation(summary = "线上支付")
    @PostMapping("/pay")
    public Response<Object> payOrder(@RequestBody @Validated OrderOnlinePayReq req) {
        return Response.success(orderService.onlinePay(req));
    }

    @Operation(summary="取消报名订单")
    @PostMapping("/cancel/{orderId}")
    public Response<Void> cancel(@PathVariable String orderId) {
        orderService.cancelOrder(orderId);
        return Response.success();
    }

    @Operation(summary = "退课订单查询")
    @GetMapping("/refund/page")
    public Response<List<StudentRefundResp>> getRefundPage(@Validated StudentRefundQuery query, PageQuery pageQuery) {
        this.checkStudentRelation(query.getStudentId());
        return Response.success(studentOrderService.getRefundPage(query, pageQuery));
    }
}
